package com.andrew.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.andrew.entity.Comment;
import com.andrew.service.CommentService;
import com.andrew.service.impl.CommentServiceImpl;
import com.google.gson.Gson;

public class getCommentByBookIdServlet extends HttpServlet {
	private Gson gson=new Gson();
	private CommentService commentService=new CommentServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置编码
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		
		//获取书本id
		int bookId=Integer.parseInt(req.getParameter("bookId"));
		//获取评论
		try {
			List<Comment> comments = commentService.getCommentByBookId(bookId);
			//返回json
			String json=gson.toJson(comments);
			System.out.println(json);
			resp.getWriter().write(json);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}
}
